#!/usr/bin/env sh
set -e

TARGET_HOST=${TARGET_HOST:-localhost}
TARGET_PORT=${TARGET_PORT:-33010}
SOURCE_DIR=${SOURCE_FILE:-/resource/}
TARGET_DIR=${TARGET_DIR:-/dist/}
RSYNC_OPTION=${RSYNC_OPTION:-""}

if nc -zv "$TARGET_HOST" "$TARGET_PORT"; then
    rsync -av ${RSYNC_OPTION} ${SOURCE_DIR} ${TARGET_DIR}
else
    echo "Error: Service $TARGET_HOST:$TARGET_PORT is unreachable" >&2
    exit 1
fi
